package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.domain.CarPackageAudit;
import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.mapper.BpmnInfoMapper;
import cn.wolfcode.car.business.mapper.CarPackageAuditMapper;
import cn.wolfcode.car.business.mapper.ServiceItemMapper;
import cn.wolfcode.car.business.query.ServiceItemQuery;
import cn.wolfcode.car.business.service.IServiceItemService;
import cn.wolfcode.car.business.vo.StartAuditVo;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ServiceItemServiceImpl implements IServiceItemService {

    @Autowired
    private ServiceItemMapper serviceItemMapper;
    @Autowired
    private CarPackageAuditMapper carPackageAuditMapper;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private BpmnInfoMapper bpmnInfoMapper;


    @Override
    public TablePageInfo<ServiceItem> query(ServiceItemQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<ServiceItem>(serviceItemMapper.selectForList(qo));
    }


    @Override
    public void save(ServiceItem serviceItem) {
        if (serviceItem.getCarPackage().equals(ServiceItem.CARPACKAGE_YES)) {
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_INIT);
        }else {
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_NO_REQUIRED);
        }
        serviceItem.setCreateTime(new Date());
        serviceItem.setSaleStatus(ServiceItem.SALESTATUS_OFF);
        serviceItemMapper.insert(serviceItem);
    }

    @Override
    public ServiceItem get(Long id) {
        return serviceItemMapper.selectByPrimaryKey(id);
    }


    @Override
    public void update(ServiceItem serviceItem) {
        ServiceItem oldService = get(serviceItem.getId());
        serviceItem.setAuditStatus(oldService.getAuditStatus());
        if (oldService.getSaleStatus().equals(ServiceItem.SALESTATUS_ON) || oldService.getAuditStatus().equals(ServiceItem.AUDITSTATUS_AUDITING)) {
            throw  new BusinessException("非法操作！");
        }
        if(oldService.getCarPackage().equals(ServiceItem.CARPACKAGE_YES) && oldService.getAuditStatus().equals(ServiceItem.AUDITSTATUS_APPROVED)){
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_INIT);
        }
        serviceItem.setSaleStatus(ServiceItem.SALESTATUS_OFF);
        serviceItemMapper.updateByPrimaryKey(serviceItem);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            serviceItemMapper.deleteByPrimaryKey(dictId);
        }
    }

    @Override
    public List<ServiceItem> list() {
        return serviceItemMapper.selectAll();
    }

    @Override
    public void saleOn(Long id) {
        ServiceItem serviceItem = get(id);
        if (!serviceItem.getSaleStatus().equals(ServiceItem.SALESTATUS_OFF)) {
            throw new BusinessException("非法操作!");
        }
        if (serviceItem.getCarPackage().equals(ServiceItem.CARPACKAGE_YES) && !serviceItem.getAuditStatus().equals(ServiceItem.AUDITSTATUS_APPROVED)) {
            throw new BusinessException("非法操作！");
        }
        serviceItemMapper.changeSalesStatus(id,ServiceItem.SALESTATUS_ON);
    }

    @Override
    public void saleOff(Long id) {
        ServiceItem serviceItem = get(id);
        if (!serviceItem.getSaleStatus().equals(ServiceItem.SALESTATUS_ON)) {
            throw new BusinessException("非法操作!");
        }
        serviceItemMapper.changeSalesStatus(id,ServiceItem.SALESTATUS_OFF);
    }

    @Override
    public void startAudit(StartAuditVo vo) {
        ServiceItem serviceItem = serviceItemMapper.selectByPrimaryKey(vo.getId());
        if(serviceItem.getAuditStatus().equals(ServiceItem.AUDITSTATUS_AUDITING) || serviceItem.getAuditStatus().equals(ServiceItem.AUDITSTATUS_APPROVED)){
            throw new BusinessException("非法操作");
        }
        if (serviceItem.getCarPackage() == ServiceItem.CARPACKAGE_NO){
            throw new BusinessException("非法操作");
        }
        serviceItemMapper.changeAuditStatus(serviceItem.getId(),ServiceItem.AUDITSTATUS_AUDITING);
        CarPackageAudit carPackageAudit = new CarPackageAudit();
        carPackageAudit.setServiceItemId(serviceItem.getId());
        carPackageAudit.setServiceItemInfo(serviceItem.getInfo());
        carPackageAudit.setServiceItemPrice(serviceItem.getDiscountPrice());
        carPackageAudit.setAuditorId(vo.getDirector());
        carPackageAudit.setBpmnInfoId(vo.getBpmnInfoId());
        carPackageAudit.setCreateTime(new Date());
        carPackageAudit.setCreator(ShiroUtils.getUser().getUserName());
        carPackageAudit.setInfo(vo.getInfo());
        carPackageAuditMapper.insert(carPackageAudit);
        Map<String,Object> map = new HashMap<>();
        map.put("discountPrice",serviceItem.getDiscountPrice().longValue());
        map.put("director",vo.getDirector());
        if (vo.getFinance() != null){
            map.put("finance",vo.getFinance());
        }
        BpmnInfo bpmnInfo = bpmnInfoMapper.selectByPrimaryKey(vo.getBpmnInfoId());
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(bpmnInfo.getActProcessKey(), carPackageAudit.getId().toString(), map);
        carPackageAudit.setInstanceId(processInstance.getId());
        carPackageAuditMapper.updateByPrimaryKey(carPackageAudit);
    }
}
